package com.example.srm.common.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.srm.domain.vo.PageView;
import com.example.srm.domain.vo.Result;
import com.example.srm.common.service.BaseIService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author pfk
 * @creatTime 2021/11/29 08:58
 * @describe    模板抽象controller类
 */
@Slf4j

public abstract class BaseController<S extends BaseIService<T>,T> {

    @Autowired
    public S baseIService;

    /**
     * 查询
     */
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public Result query(Integer page,Integer pageSize){
        //校验参数是否合法
        if (page==null||page<0){
            page=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=10;
        }

        Page<T> tPage = baseIService.page(new Page<>(page,pageSize));

        PageView<T> pageView = new PageView<T>()
                .setRows(tPage.getRecords())
                .setTotalCount(tPage.getTotal())
                .setTotalPage(new Integer(tPage.getPages()+""));

        return new Result().setData(pageView).setSuccess(true);
    }

    /**
     * 保存
     * @param t
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public Result save(@RequestBody T t, HttpServletRequest request) throws Exception {
        try {
            beforeSave(t);
        }catch (Exception e){
            log.error("保存失败",e);
            return new Result().setMsg("保存失败，请检查参数是否合法."+e.getMessage());
        }
        boolean save = baseIService.save(t);
        return new Result().setSuccess(save);
    }

    /**
     * 修改
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "",method = RequestMethod.PUT)
    public Result update(@RequestBody T t ,HttpServletRequest request) throws Exception {
        try {
            //判断修改的角色是否存在
            beforeUpdate(t);
        }catch (Exception e){
            log.error("修改失败",e);
            return new Result().setMsg("修改失败，"+e.getMessage());
        }
        boolean updateById = baseIService.updateById(t);
        return new Result().setSuccess(updateById);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "",params = {"id"},method = RequestMethod.GET)
    public Result findById(String id){
        T t = baseIService.getById(id);
        return new Result().setData(t).setSuccess(true);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "",params = {"id"},method = RequestMethod.DELETE)
    public Result delete(String id){
        try {
            T t = baseIService.getById(id);
            if (t==null) {
                return new Result().setMsg("删除失败，实例不存在");
            }
            boolean remove = baseIService.removeById(id);
            return new Result().setSuccess(remove);
        }catch (Exception e){
            return new Result().setSuccess(true).setMsg("删除失败，请先删除完数据字典明细");
        }
    }


    /**
     * 插入前的操作【一般是对实体类参数的检验】
     * @param t
     */
    public void beforeSave(T t) throws Exception {

    }

    /**
     * 更新前的操作【一般是对实体类参数的检验】
     * @param t
     */
    public void beforeUpdate(T t)  throws Exception {

    }


}
